<?php

namespace Admin\Controller;
use Think\Controller;

class NoticeController extends Controller{

    /*公告添加*/

    public function add(){

    	$title = I('post.title');
    	$time = time();
    	$title1 = I('post.title1');
    	$title2 = I('post.title2');
    	$title0 = I('post.title0');

    	$titlea='';

    	if($title0!=null){
    		$titlea.=$title0;
    	}
    	if($title1!=null){
    		$titlea.=';'.$title1;
    	}
    	if($title2!=null){
    		$titlea.=';'.$title2;
    	}
    	


    	$content1 = I('post.content1');
    	$content2 = I('post.content2');
    	$content0 = I('post.content0');

    	$content='';

    	if($content0!=null){
    		$content.=$content0;
    	}
    	if($content1!=null){
    		$content.=';'.$content1;
    	}
    	if($content2!=null){
    		$content.=';'.$content2;
    	}
    	

    	$data['title'] = $title;
    	$data['login_time'] = $time;
    	$data['titlea'] = $titlea;
    	$data['content'] = $content;

    	$notice = M('notice');

    	$result = $notice->add($data);

    	if($result){

    		$ajaxReturn['code'] = 1;
    		$ajaxReturn['msg'] = 'success';

    	}else{

    		$ajaxReturn['code'] = 2;
    		$ajaxReturn['msg'] = '添加失败';

    	}

    	$this->ajaxReturn($ajaxReturn);


    }

    //修改公告

    public function infos(){

    	$id = I('post.id');

    	$notice = M('notice');

    	$result = $notice->where('id='.$id)->find();

    	$titlea = explode(';',$result['titlea']);
    	$content = explode(';',$result['content']);

    	$data['id'] = $id;
    	$data['title'] = $result['title'];
    	foreach($titlea as $key=>$v){
    		$data['title'.$key] = $v;
    	}
    	foreach($content as $key=>$v){
    		$data['content'.$key] = $v;
    	}
    	
    	$ajaxReturn['code'] = 1;
    	$ajaxReturn['msg'] = 'success';
    	$ajaxReturn['data'] = $data;
    	$this->ajaxReturn($ajaxReturn);

    }

    public function save(){

    	$id = I('post.id');

		$title = I('post.title');

    	$title1 = I('post.title1');
    	$title2 = I('post.title2');
    	$title0 = I('post.title0');

    	$content0 = I('post.content0');
    	$content1 = I('post.content1');
    	$content2 = I('post.content2');
    	

    	$data['titlea'] = $title0.';'.$title1.';'.$title2;
		$data['content'] = $content0.';'.$content1.';'.$content2;
		$data['title'] = $title;

		$notice = M('notice');

    	$result = $notice->where('id='.$id)->save($data);

    	if($result){

    		$ajaxReturn['code'] = 1;
    		$ajaxReturn['msg'] = 'success';

    	}else{

    		$ajaxReturn['code'] = 2;
    		$ajaxReturn['msg'] = '修改失败';

    	}

    	$this->ajaxReturn($ajaxReturn);
    }


    /*删除公告*/

    public function delete(){

    	$id = I('post.id');

    	$notice = M('notice');

    	$result = $notice->where('id='.$id)->delete();

    	if($result){

    		$ajaxReturn['code'] = 1;
    		$ajaxReturn['msg'] = 'success';

    	}else{

    		$ajaxReturn['code'] = 2;
    		$ajaxReturn['msg'] = '删除失败';

    	}

    	$this->ajaxReturn($ajaxReturn);
    }

    /*查看公告*/

    public function show(){

    	$page = I('post.page');
    	$page_size = I('post.page_size');

    	$notice = M('notice');

    	$count = $notice->count();
        $total =  ceil($count / $page_size);
        $limitStart = ($page-1) * $page_size;
        
        $result=$notice->order("id desc")->limit($limitStart,$page_size)->select();

    	foreach($result as $key=>$v){

    /*		$content = explode(';',$v['content']);

    		$data[$key]['content'] = $content;


    		$titlea = explode(';',$v['titlea']);

    		$data[$key]['titlea'] = $titlea;*/

            $res[$key]['id'] = $v['id'];
    		$res[$key]['time'] = $v['login_time'];
    		$res[$key]['title'] = $v['title'];


    	}
    	
    	if($res !== false){

            $data['page']['total_page'] = $total;
            $data['list'] = $res;
            

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $data;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);

    }


}